package org.iabako.client.ui.form.business.report;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.layout.field.InputTextDate;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.shared.dto.ReportByEnum;
import org.iabako.shared.iservice.report.ProfitReportGeneratorServiceAsync;
import org.iabako.shared.tools.GenericTools;

import java.util.Date;

/**
 * Created by jose on 28/01/15.
 */
public class ProfitReportForm extends AbstractReportForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, ProfitReportForm> {
    }

    private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

    protected final ProfitReportGeneratorServiceAsync profitReportGeneratorServiceAsync = ProfitReportGeneratorServiceAsync.Util.getInstance();

    @UiField
    InputTextDate profitsAfter;
    @UiField
    InputTextDate profitsBefore;

    @UiConstructor
    public ProfitReportForm() {
        initWidget(binder.createAndBindUi(this));

        setHandlers();

        setDefaultValues();
    }

    private void setHandlers() {
        generateReportAction.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                if (validateMandatoryFields()) {

                    reportGenerationInputDTO.setReportByEnum(ReportByEnum.byProfit);
                    reportGenerationInputDTO.setPeriod1DateAfter(GenericTools.formatDateToString(profitsAfter.getDateValue()));
                    reportGenerationInputDTO.setPeriod1DateBefore(GenericTools.formatDateToString(profitsBefore.getDateValue()));

                    profitReportGeneratorServiceAsync.generateProfitReport(reportGenerationInputDTO, new IabakoAsyncCallback<String>() {

                        public void onSuccess(String result) {
                            generateForm.setMethod("GET");
                            reportGeneratorFileToken.setValue(result);
                            generateForm.submit();
                            super.onSuccess(result);
                        }
                    });
                }

            }
        });
    }

    private boolean validateMandatoryFields() {
        if (GenericTools.isEmpty(profitsAfter.getDateValue(), profitsBefore.getDateValue())) {

            ValidationMessage.getFirstFromDOM().showErrorMessage(true, true, "report_profits_dates_mandatory");
            return false;

        } else if (profitsAfter.getDateValue().compareTo(profitsBefore.getDateValue()) > 0) {

            ValidationMessage.getFirstFromDOM().showErrorMessage(true, true, "report_installments_dates_error");
            return false;
        }

        return true;
    }

    private void setDefaultValues() {
        Date date = GenericTools.getDateWithoutTime();
        date.setDate(1);
        profitsAfter.setValue(date);
        profitsBefore.setValue(GenericTools.getDateWithoutTime());
    }
}
